package com.ybkj.daijia.server.event.common.listener;

import com.ybkj.daijia.OrderNumberConfig;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.abstractModels.Order;
import com.ybkj.daijia.server.event.common.event.IntroduceRebateEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.mc.PassengerAccount;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverPaymentsRecord;
import com.ybkj.daijia.server.model.DriverRebate;
import com.ybkj.daijia.server.model.FinancialRecord;
import com.ybkj.daijia.server.model.IntroduceRecord;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverPaymentsRecordService;
import com.ybkj.daijia.server.service.DriverRebateService;
import com.ybkj.daijia.server.service.DriverRechargeRecordService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.FinancialRecordService;
import com.ybkj.daijia.server.service.IntroduceRecordService;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberPaymentsRecordService;
import com.ybkj.daijia.server.service.MemberRebateService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.OrderFinishedInfoService;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class IntroduceRebateEventListener implements ApplicationListener<IntroduceRebateEvent> {

    public static final String LEFT = "（";
    public static final String RIGHT = "）";
    private static final Logger logger = LoggerFactory
        .getLogger(IntroduceRebateEventListener.class);
    @Autowired
    private MemberService memberService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private MemberAccountService memberAccountService;
    @Autowired
    private MemberRebateService memberRebateService;
    @Autowired
    private DriverRebateService driverRebateService;
    @Autowired
    private FinancialRecordService financialRecordService;
    @Autowired
    private MemberPaymentsRecordService memberPaymentsRecordService;
    @Autowired
    private DriverRechargeRecordService driverRechargeRecordService;
    @Autowired
    private OrderFinishedInfoService orderFinishedInfoService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private SettingUtils settingUtils;
    @Autowired
    private DriverPaymentsRecordService driverPaymentsRecordService;
    @Autowired
    private IntroduceRecordService introduceRecordService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onApplicationEvent(IntroduceRebateEvent event) {

        logger.debug("MemberRebateEvent event:{}", "");
   /*     try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        Passenger passenger = (Passenger) event.getSource();
        Order order = event.getOrder();

        BigDecimal realCash = event.getRealCash();
        Integer type = event.getType();//0一次性 1代驾  其中代价结算都是1
        Integer introduceType = passenger.getIntroduceType();//0客户推荐 1服务人员推荐
        if (null != introduceType) {
            if (introduceType.equals(0)) {//客户推荐
//                MemberRebate memberRebate = memberRebateService.findByCompany(passenger.getCompanyId());
//                if (type.equals(1)) {
//                    if (null != memberRebate && memberRebate.getType().equals(0) && passenger.getReward() == 1) {
//                        if (StringUtils.isNotBlank(passenger.getIntroducer())) {
//                            //第一级
//                            Passenger firstPassenger = memberService.findOnePassengerByPhone(passenger.getIntroducer());
//                            if (null != firstPassenger && memberRebate.getLevel() >= 1) {
////								Member first=memberService.findOne(firstAccount.getMemberId());
//                                IntroduceRecord introduceRecord = new IntroduceRecord();
//                                introduceRecord.setPassengerPhone(passenger.getPhone());
//                                introduceRecord.setPassengerIntroducePhone(firstPassenger.getPhone());
//                                Integer integer = introduceRecordService.countByPhone(introduceRecord);
//                                if (firstPassenger.getCompanyId() == passenger.getCompanyId()
//                                        && null != memberRebate.getDisposableMoney1()
//                                        && memberRebate.getDisposableMoney1().compareTo(BigDecimal.ZERO) == 1
//                                        && integer == 0) {
////									firstPassenger.setDrawMoney(firstPassenger.getDrawMoney()+memberRebate.getDisposableMoney1().doubleValue());
////									memberService.addVirtual(memberRebate.getDisposableMoney1(), firstPassenger.getId());
//                                    introduceRecordService.insertRecord(introduceRecord);
//                                    saveFinancialRecord(passenger, firstPassenger, null, memberRebate.getDisposableMoney1(), 1, type);
//                                }
//                                //第二级
//                                Passenger secondPassenger = memberService.findOnePassengerByPhone(firstPassenger.getIntroducer());
//                                if (null != secondPassenger && memberRebate.getLevel() >= 2) {
////									Member second=memberService.findOne(secondAccount.getMemberId());
//                                    IntroduceRecord introduceRecord1 = new IntroduceRecord();
//                                    introduceRecord1.setPassengerPhone(passenger.getPhone());
//                                    introduceRecord1.setPassengerIntroducePhone(secondPassenger.getPhone());
//                                    Integer integer1 = introduceRecordService.countByPhone(introduceRecord1);
//                                    if (secondPassenger.getCompanyId() == passenger.getCompanyId()
//                                            && null != memberRebate.getDisposableMoney2()
//                                            && memberRebate.getDisposableMoney2().compareTo(BigDecimal.ZERO) == 1
//                                            && integer1 == 0) {
////										secondPassenger.setDrawMoney(secondPassenger.getDrawMoney()+memberRebate.getDisposableMoney2().doubleValue());
////										memberService.addVirtual(memberRebate.getDisposableMoney2(), secondPassenger.getId());
//                                        introduceRecordService.insertRecord(introduceRecord1);
//                                        saveFinancialRecord(passenger, secondPassenger, null, memberRebate.getDisposableMoney2(), 2, type);
//                                    }
//                                    //第三级
//                                    Passenger thirdPassenger = memberService.findOnePassengerByPhone(secondPassenger.getIntroducer());
//                                    if (null != thirdPassenger && memberRebate.getLevel() >= 3) {
////										Member third=memberService.findOne(thirdAccount.getMemberId());
//                                        IntroduceRecord introduceRecord2 = new IntroduceRecord();
//                                        introduceRecord2.setPassengerPhone(passenger.getPhone());
//                                        introduceRecord2.setPassengerIntroducePhone(thirdPassenger.getPhone());
//                                        Integer integer2 = introduceRecordService.countByPhone(introduceRecord2);
//                                        if (thirdPassenger.getCompanyId() == passenger.getCompanyId()
//                                                && null != memberRebate.getDisposableMoney3()
//                                                && memberRebate.getDisposableMoney3().compareTo(BigDecimal.ZERO) == 1
//                                                && integer2 == 0) {
////											thirdPassenger.setDrawMoney(thirdPassenger.getDrawMoney()+memberRebate.getDisposableMoney3().doubleValue());
////											memberService.addVirtual(memberRebate.getDisposableMoney3(), thirdPassenger.getId());
//                                            introduceRecordService.insertRecord(introduceRecord2);
//                                            saveFinancialRecord(passenger, thirdPassenger, null, memberRebate.getDisposableMoney3(), 3, type);
//                                        }
//                                    }
//                                }
//                            }
//                        }
//                    }
//                    if (null != memberRebate && memberRebate.getType().equals(1)) {
//                        if (StringUtils.isNotBlank(passenger.getIntroducer())) {
//                            //第一级
//                            Passenger firstPassenger = memberService.findOnePassengerByPhone(passenger.getIntroducer());
//                            if (null != firstPassenger && memberRebate.getLevel() >= 1) {
//                                IntroduceRecord introduceRecord = new IntroduceRecord();
//                                introduceRecord.setPassengerPhone(passenger.getPhone());
//                                introduceRecord.setPassengerIntroducePhone(firstPassenger.getPhone());
//                                Integer integer = introduceRecordService.countByPhone(introduceRecord);
////								Passenger first=memberService.findOne(firstAccount.getMemberId());
//                                if (firstPassenger.getCompanyId() == passenger.getCompanyId()) {
//                                    if (memberRebate.getMemberRebateType1().equals(MemberRebate.MemberRebateType.guding)) {
//                                        if (null != memberRebate.getMoney1() && memberRebate.getMoney1().compareTo(BigDecimal.ZERO) == 1) {
////											first.setVirtual(first.getVirtual().add(memberRebate.getMoney1()));
////											memberService.addVirtual(memberRebate.getMoney1(), first.getId());
//                                            if (integer == 0) {
//                                                introduceRecordService.insertRecord(introduceRecord);
//                                            }
//                                            saveFinancialRecord(passenger, firstPassenger, order, memberRebate.getMoney1(), 1, type);
//                                        }
//                                    } else if (memberRebate.getMemberRebateType1().equals(MemberRebate.MemberRebateType.percent)) {
//                                        if (null != memberRebate.getCommission1() && memberRebate.getCommission1().compareTo(BigDecimal.ZERO) == 1) {
//                                            BigDecimal money = realCash.multiply(memberRebate.getCommission1()).divide(new BigDecimal(100));
////											first.setVirtual(first.getVirtual().add(money));
////											memberService.addVirtual(money, first.getId());
//                                            if (integer == 0) {
//                                                introduceRecordService.insertRecord(introduceRecord);
//                                            }
//                                            saveFinancialRecord(passenger, firstPassenger, order, money, 1, type);
//                                        }
//                                    }
//                                }
//                                //第二级
//                                Passenger secondPassenger = memberService.findOnePassengerByPhone(firstPassenger.getIntroducer());
//                                if (null != secondPassenger && memberRebate.getLevel() >= 2) {
//                                    IntroduceRecord introduceRecord1 = new IntroduceRecord();
//                                    introduceRecord1.setPassengerPhone(passenger.getPhone());
//                                    introduceRecord1.setPassengerIntroducePhone(secondPassenger.getPhone());
//                                    Integer integer1 = introduceRecordService.countByPhone(introduceRecord1);
////									Member second=memberService.findOne(secondAccount.getMemberId());
//                                    if (secondPassenger.getCompanyId() == passenger.getCompanyId()) {
//                                        if (memberRebate.getMemberRebateType2().equals(MemberRebate.MemberRebateType.guding)) {
//                                            if (null != memberRebate.getMoney2() && memberRebate.getMoney2().compareTo(BigDecimal.ZERO) == 1) {
////												second.setVirtual(second.getVirtual().add(memberRebate.getMoney2()));
////												memberService.addVirtual(memberRebate.getMoney2(), second.getId());
//                                                if (integer1 == 0) {
//                                                    introduceRecordService.insertRecord(introduceRecord1);
//                                                }
//                                                saveFinancialRecord(passenger, secondPassenger, order, memberRebate.getMoney2(), 2, type);
//                                            }
//                                        } else if (memberRebate.getMemberRebateType2().equals(MemberRebate.MemberRebateType.percent)) {
//                                            if (null != memberRebate.getCommission2() && memberRebate.getCommission2().compareTo(BigDecimal.ZERO) == 1) {
//                                                BigDecimal money = realCash.multiply(memberRebate.getCommission2()).divide(new BigDecimal(100));
////												second.setVirtual(second.getVirtual().add(money));
////												memberService.addVirtual(money, second.getId());
//                                                if (integer1 == 0) {
//                                                    introduceRecordService.insertRecord(introduceRecord1);
//                                                }
//                                                saveFinancialRecord(passenger, secondPassenger, order, money, 2, type);
//                                            }
//                                        }
//                                    }
//                                    //第三级
//                                    Passenger thirdPassenger = memberService.findOnePassengerByPhone(secondPassenger.getIntroducer());
//                                    if (null != thirdPassenger && memberRebate.getLevel() >= 3) {
//                                        IntroduceRecord introduceRecord2 = new IntroduceRecord();
//                                        introduceRecord2.setPassengerPhone(passenger.getPhone());
//                                        introduceRecord2.setPassengerIntroducePhone(thirdPassenger.getPhone());
//                                        Integer integer2 = introduceRecordService.countByPhone(introduceRecord2);
////										Member third=memberService.findOne(thirdAccount.getMemberId());
//                                        if (thirdPassenger.getCompanyId() == passenger.getCompanyId()) {
//                                            if (memberRebate.getMemberRebateType3().equals(MemberRebate.MemberRebateType.guding)) {
//                                                if (null != memberRebate.getMoney3() && memberRebate.getMoney3().compareTo(BigDecimal.ZERO) == 1) {
////													third.setVirtual(third.getVirtual().add(memberRebate.getMoney3()));
////													memberService.addVirtual(memberRebate.getMoney3(), third.getId());
//                                                    if (integer2 == 0) {
//                                                        introduceRecordService.insertRecord(introduceRecord2);
//                                                    }
//                                                    saveFinancialRecord(passenger, thirdPassenger, order, memberRebate.getMoney3(), 3, type);
//                                                }
//                                            } else if (memberRebate.getMemberRebateType3().equals(MemberRebate.MemberRebateType.percent)) {
//                                                if (null != memberRebate.getCommission3() && memberRebate.getCommission3().compareTo(BigDecimal.ZERO) == 1) {
//                                                    BigDecimal money = realCash.multiply(memberRebate.getCommission3()).divide(new BigDecimal(100));
////													third.setVirtual(third.getVirtual().add(money));
////													memberService.addVirtual(money, third.getId());
//                                                    if (integer2 == 0) {
//                                                        introduceRecordService.insertRecord(introduceRecord2);
//                                                    }
//                                                    saveFinancialRecord(passenger, thirdPassenger, order, money, 3, type);
//                                                }
//                                            }
//                                        }
//
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
            } else if (introduceType.equals(1)) {//服务人员推荐提成
                if (order.getChannelName()
                    .equals(com.ybkj.daijia.server.zhuanxian.Order.ORDER_TYPE_BUDAN) || order
                    .getChannelName()
                    .equals(com.ybkj.daijia.server.zhuanxian.Order.ORDER_TYPE_PHONE)) {
                    return;
                }
                Driver firstDriver = driverService.findByUsername(passenger.getIntroducer());
                DriverRebate driverRebate = driverRebateService.findByDriverId(firstDriver.getId());
                if (null != driverRebate) {

                    Driver thisDr = driverService.findOne(order.getEmployId());
                    if (thisDr.getId() != firstDriver.getId()) {
                        BigDecimal money = realCash.multiply(driverRebate.getCommission1())
                            .divide(new BigDecimal(100));

                        thisDr.setVirtual(thisDr.getVirtual().subtract(money));
                        driverService.updateVirtual(thisDr);

                        firstDriver.setVirtual(firstDriver.getVirtual().add(money));
                        driverService.updateVirtual(firstDriver);

                        IntroduceRecord introduceRecord = new IntroduceRecord();
                        introduceRecord.setPassengerPhone(passenger.getPhone());
                        introduceRecord.setPassengerIntroducePhone(firstDriver.getPhone());
                        introduceRecordService.insertRecord(introduceRecord);

                        saveDriversRebateFinacialRecord(firstDriver, passenger, money, order);
                        saveDriversRebateFinacialRecord(thisDr, passenger, money.negate(), order);
                    }
                }
//				if(type.equals(1)){
//					if(null!=driverRebate && driverRebate.getType().equals(0)&&passenger.getReward()==1){
//						if(StringUtils.isNotBlank(passenger.getIntroducer())){
//							//第一级
//                            IntroduceRecord introduceRecord = new IntroduceRecord();
//                            introduceRecord.setPassengerPhone(passenger.getPhone());
//                            introduceRecord.setPassengerIntroducePhone(firstDriver.getPhone());
//                            Integer integer = introduceRecordService.countByPhone(introduceRecord);
//                            if(null!=firstDriver && driverRebate.getLevel()>=1){
//                                if(firstDriver.getCompanyId()==passenger.getCompanyId()
//                                        && null!=driverRebate.getDisposableMoney1()
//                                        && driverRebate.getDisposableMoney1().compareTo(BigDecimal.ZERO)==1
//                                        && integer==0){
//                                    firstDriver.setVirtual(firstDriver.getVirtual().add(driverRebate.getDisposableMoney1()));
//                                    driverService.updateVirtual(firstDriver);
//                                    introduceRecordService.insertRecord(introduceRecord);
//                                    saveDriverFinancialRecord(passenger,firstDriver,null,driverRebate.getDisposableMoney1(),1,type);
//                                }
//                            }
//                        }
//                    }
//					if(null!=driverRebate && driverRebate.getType().equals(1)){
//						if(StringUtils.isNotBlank(passenger.getIntroducer())){
//							//第一级
//							if(null!=firstDriver && driverRebate.getLevel()>=1){
//                                IntroduceRecord introduceRecord = new IntroduceRecord();
//                                introduceRecord.setPassengerPhone(passenger.getPhone());
//                                introduceRecord.setPassengerIntroducePhone(firstDriver.getPhone());
//                                Integer integer = introduceRecordService.countByPhone(introduceRecord);
//								if(firstDriver.getCompanyId()==passenger.getCompanyId()){
//									if(driverRebate.getDriverRebateType1().equals(DriverRebate.DriverRebateType.guding)){
//										if(null!=driverRebate.getMoney1() && driverRebate.getMoney1().compareTo(BigDecimal.ZERO)==1){
//											firstDriver.setVirtual(firstDriver.getVirtual().add(driverRebate.getMoney1()));
//											driverService.updateVirtual(firstDriver);
//											if(integer==0){
//                                                introduceRecordService.insertRecord(introduceRecord);
//                                            }
//											saveDriverFinancialRecord(passenger,firstDriver,order,driverRebate.getMoney1(),1,type);
//										}
//									}
//									else if(driverRebate.getDriverRebateType1().equals(DriverRebate.DriverRebateType.percent)){
//										if(null!=driverRebate.getCommission1() && driverRebate.getCommission1().compareTo(BigDecimal.ZERO)==1){
//											BigDecimal money=realCash.multiply(driverRebate.getCommission1()).divide(new BigDecimal(100));
//											firstDriver.setVirtual(firstDriver.getVirtual().add(money));
//											driverService.updateVirtual(firstDriver);
//                                            if(integer==0){
//                                                introduceRecordService.insertRecord(introduceRecord);
//                                            }
//											saveDriverFinancialRecord(passenger,firstDriver,order,money,1,type);
//										}
//									}
//								}
//
//							}
//						}
//					}
//				}
            }
        }

    }

    /****
     *  通过推荐提成相关司机明细记录,获取被推荐人的电话号码
     * @param record
     * @return
     */
    public String getPassengerPhoneFromDriverPaymentRecord(DriverPaymentsRecord record) {
        if (null == record) {
            return null;
        }
        if (DriverPaymentsRecord.RECHARGE_TYPE_REBATE.equals(record.getRecordType())
            || DriverPaymentsRecord.DELETE_TYPE_REBATE.equals(record.getRecordType())) {
            String memo = record.getMemo();
            if (null != memo) {
                int start = memo.indexOf(LEFT);
                int end = memo.indexOf(RIGHT);
                if (start != -1 && -1 != end) {
                    return memo.substring(start + 1, end);
                }
            }
        }
        return null;
    }

    public void saveDriversRebateFinacialRecord(Driver addDriver, Passenger passenger,
        BigDecimal money, Order order) {
        FinancialRecord financialRecord = new FinancialRecord();
        Company company = companyService.findOne(addDriver.getCompanyId());
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        StringBuilder sb = new StringBuilder();
        sb.append(OrderNumberConfig.FINANCIAL_MEMBERREBATE);//推荐提成
        sb.append(OrderNumberConfig.FINANCIAL_WHO_SYS);//系统添加
        sb.append(format.format(new Date()));//年月日时间
        sb.append(OrderNumberConfig.padZero(financialRecordService.getRunningNumber() + 1));//流水号
        financialRecord.setCode(sb.toString());
        financialRecord.setCompanyId(company.getId());
        financialRecord.setCompanyName(company.getAbbreviation());
        financialRecord.setStatus(FinancialRecord.FINANCIALRECORD_OK);
        if (money.compareTo(BigDecimal.ZERO) > 0) {
            financialRecord.setType(FinancialRecord.FINANCIALRECORD_TYPE_MEMBERREBATE);
        } else {
            financialRecord.setType(FinancialRecord.FINANCIALRECORD_TYPE_SUB_MEMBERREBATE);
        }
        money = money.setScale(2);
        financialRecord.setMoney(money);
        if (null != order) {
            financialRecord.setOrderId(order.getId());
            Company orderCompany = companyService.findOne(order.getCompanyId());
            financialRecord.setOrderCompanyId(orderCompany.getId());
            financialRecord.setOrderCompanyName(orderCompany.getAbbreviation());
        }
        String memo = null;
        if (money.compareTo(BigDecimal.ZERO) > 0) {
            memo =
                "客户【" + passenger.getName() + "（" + passenger.getPhone() + "）】结单,服务人员【" + addDriver
                    .getRealname() + "】获得结单推荐提成" + money + "元";
        } else {
            memo =
                "客户【" + passenger.getName() + "（" + passenger.getPhone() + "）】结单,服务人员【" + addDriver
                    .getRealname() + "】扣除结单推荐提成" + money + "元";
        }
        if (null != order) {
            memo += ",单号：" + order.getOrderNo();
        }
        financialRecord.setMemo(memo);
        financialRecord.setWho(addDriver.getRealname());
        financialRecordService.save(financialRecord);
        //服务人员财务
        DriverPaymentsRecord driverPaymentsRecord = new DriverPaymentsRecord();
        driverPaymentsRecord.setMoney(money);
        driverPaymentsRecord.setAfterMoney(addDriver.getVirtual());
        String memo2 = null;
        if (money.compareTo(BigDecimal.ZERO) > 0) {
            memo2 =
                "客户【" + passenger.getName() + "（" + passenger.getPhone() + "）】结单,获得结单推荐提成" + money
                    + "元";
            driverPaymentsRecord.setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_REBATE);
            driverPaymentsRecord.setRcType(DriverPaymentsRecord.DIFFERENT_POSITIVE);
        } else {
            memo2 =
                "客户【" + passenger.getName() + "（" + passenger.getPhone() + "）】结单,扣除结单推荐提成" + money
                    + "元";
            driverPaymentsRecord.setRecordType(DriverPaymentsRecord.DELETE_TYPE_REBATE);
            driverPaymentsRecord.setRcType(DriverPaymentsRecord.DIFFERENT_NEGATIVE);
        }
        driverPaymentsRecord.setDriverId(addDriver.getId());
        driverPaymentsRecord.setDriverRealName(addDriver.getRealname());
        driverPaymentsRecord.setDriverUserName(addDriver.getUsername());
        driverPaymentsRecord.setDriverPhone(addDriver.getPhone());
        driverPaymentsRecord.setDriverCompanyId(addDriver.getCompanyId());
        if (null != order) {
            driverPaymentsRecord.setOrderId(order.getId());
            driverPaymentsRecord.setOrderNumber(order.getOrderNo());
            driverPaymentsRecord.setOrderType(DriverPaymentsRecord.ORDER_TYPE_ZHUANXIAN);
            driverPaymentsRecord.setOrderCompanyId(order.getCompanyId());
        }

        if (null != order) {
            memo2 += ",单号：" + order.getOrderNo();
        }
        driverPaymentsRecord.setMemo(memo2);
        driverPaymentsRecord.setOperating(DriverPaymentsRecord.OPERATING_SYS);

        driverPaymentsRecord.setHappend(new Date());
        driverPaymentsRecordService.insertRecord(driverPaymentsRecord);
    }

    public void saveFinancialRecord(Passenger sourcePassenger, Passenger introducer, Order order,
        BigDecimal money, int setup, int type) {
        FinancialRecord financialRecord = new FinancialRecord();
        Company company = companyService.findOne(introducer.getCompanyId());
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        StringBuilder sb = new StringBuilder();
        sb.append(OrderNumberConfig.FINANCIAL_MEMBERREBATE);//推荐提成
        sb.append(OrderNumberConfig.FINANCIAL_WHO_SYS);//系统添加
        sb.append(format.format(new Date()));//年月日时间
        sb.append(OrderNumberConfig.padZero(financialRecordService.getRunningNumber() + 1));//流水号
        financialRecord.setCode(sb.toString());
        financialRecord.setCompanyId(company.getId());
        financialRecord.setCompanyName(company.getAbbreviation());
        financialRecord.setStatus(FinancialRecord.FINANCIALRECORD_OK);
        financialRecord.setType(FinancialRecord.FINANCIALRECORD_TYPE_MEMBERREBATE);
        financialRecord.setMoney(money.multiply(new BigDecimal(-1)));
        if (null != order) {
            financialRecord.setOrderId(order.getId());
            Company orderCompany = companyService.findOne(order.getCompanyId());
            financialRecord.setOrderCompanyId(orderCompany.getId());
            financialRecord.setOrderCompanyName(orderCompany.getAbbreviation());
        }
        if (type == 1) {
            String memo =
                "客户【" + sourcePassenger.getName() + "（" + sourcePassenger.getPhone() + "）】结单,【"
                    + introducer.getName() + "】获得结单" + setup + "级推荐提成" + money + "元";
            if (null != order) {
                memo += ",单号：" + order.getOrderNo();
            }
            financialRecord.setMemo(memo);
        }
        financialRecord.setWho(introducer.getName());
        financialRecordService.save(financialRecord);
        String memo = "";
        if (type == 1) {
            memo =
                "客户【" + sourcePassenger.getName() + "（" + sourcePassenger.getPhone() + "）】结单,获得结单"
                    + setup + "级推荐提成" + money + "元";
            if (null != order) {
                memo += ",单号：" + order.getOrderNo();
            }
        }
        Long orderId = null;
        String orderNumber = null;
        Integer orderType = null;
        if (null != order) {
            orderId = order.getId();
            orderNumber = order.getOrderNo();
            orderType = PassengerAccount.ORDER_TYPE_ZHUANXIAN;
        }
        memberService
            .money(introducer, sourcePassenger.getId(), money, memo, PassengerAccount.OPERATING_SYS,
                PassengerAccount.PayType.RECHARGE_TYPE_REBATE, orderId, orderNumber, orderType,
                null, true, false);
	/*	if(null!=order){
			OrderFinishedInfo orderFinishedInfo=orderFinishedInfoService.findByOrder(order.getId());
			if(null!=orderFinishedInfo){
				orderFinishedInfo.setRebateMoney(orderFinishedInfo.getRebateMoney().add(money));
				orderFinishedInfoService.update(orderFinishedInfo);
			}
		}*/
    }

    public void saveDriverFinancialRecord(Passenger sourcePassenger, Driver driver, Order order,
        BigDecimal money, int setup, int type) {
        FinancialRecord financialRecord = new FinancialRecord();
        Company company = companyService.findOne(driver.getCompanyId());
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        StringBuilder sb = new StringBuilder();
        sb.append(OrderNumberConfig.FINANCIAL_MEMBERREBATE);//推荐提成
        sb.append(OrderNumberConfig.FINANCIAL_WHO_SYS);//系统添加
        sb.append(format.format(new Date()));//年月日时间
        sb.append(OrderNumberConfig.padZero(financialRecordService.getRunningNumber() + 1));//流水号
        financialRecord.setCode(sb.toString());
        financialRecord.setCompanyId(company.getId());
        financialRecord.setCompanyName(company.getAbbreviation());
        financialRecord.setStatus(FinancialRecord.FINANCIALRECORD_OK);
        financialRecord.setType(FinancialRecord.FINANCIALRECORD_TYPE_MEMBERREBATE);
        financialRecord.setMoney(money.multiply(new BigDecimal(-1)));
        if (null != order) {
            financialRecord.setOrderId(order.getId());
            Company orderCompany = companyService.findOne(order.getCompanyId());
            financialRecord.setOrderCompanyId(orderCompany.getId());
            financialRecord.setOrderCompanyName(orderCompany.getAbbreviation());
        }
        if (type == 1) {
            String memo =
                "客户【" + sourcePassenger.getName() + "（" + sourcePassenger.getPhone() + "）】结单,服务人员【"
                    + driver.getRealname() + "】获得结单" + setup + "级推荐提成" + money + "元";
            if (null != order) {
                memo += ",单号：" + order.getOrderNo();
            }
            financialRecord.setMemo(memo);
        }
        financialRecord.setWho(driver.getRealname());
        financialRecordService.save(financialRecord);
        //服务人员财务
        DriverPaymentsRecord driverPaymentsRecord = new DriverPaymentsRecord();
        driverPaymentsRecord.setMoney(money);
        driverPaymentsRecord.setAfterMoney(driver.getVirtual());
        driverPaymentsRecord.setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_REBATE);
        driverPaymentsRecord.setDriverId(driver.getId());
        driverPaymentsRecord.setDriverRealName(driver.getRealname());
        driverPaymentsRecord.setDriverUserName(driver.getUsername());
        driverPaymentsRecord.setDriverPhone(driver.getPhone());
        driverPaymentsRecord.setDriverCompanyId(driver.getCompanyId());
        if (null != order) {
            driverPaymentsRecord.setOrderId(order.getId());
            driverPaymentsRecord.setOrderNumber(order.getOrderNo());
            driverPaymentsRecord.setOrderType(DriverPaymentsRecord.ORDER_TYPE_DAIJIA);
            driverPaymentsRecord.setOrderCompanyId(order.getCompanyId());
        }
        if (type == 1) {
            String memo =
                "客户【" + sourcePassenger.getName() + "（" + sourcePassenger.getPhone() + "）】结单,获得结单"
                    + setup + "级推荐提成" + money + "元";
            if (null != order) {
                memo += ",单号：" + order.getOrderNo();
            }
            driverPaymentsRecord.setMemo(memo);
        }
        driverPaymentsRecord.setOperating(DriverPaymentsRecord.OPERATING_SYS);
        driverPaymentsRecord.setRcType(DriverPaymentsRecord.DIFFERENT_POSITIVE);
        driverPaymentsRecord.setHappend(new Date());
        driverPaymentsRecordService.insertRecord(driverPaymentsRecord);
		/*if(null!=order){
			OrderFinishedInfo orderFinishedInfo=orderFinishedInfoService.findByOrder(order.getId());
			if(null!=orderFinishedInfo){
				orderFinishedInfo.setRebateMoney(orderFinishedInfo.getRebateMoney().add(driverPaymentsRecord.getMoney()));
				orderFinishedInfoService.update(orderFinishedInfo);
			}
		}*/
    }
}
